<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class GlobalVariable extends Basic
{
    public $variable_name_repeat;
    
    public static function tableName()
    {
        return '{{%global_variable}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['lang', 'required', 'message' => 'lang不能为空', 'on' => ['add']],
            ['lang', 'string', 'message' => 'lang必须是字符串',
                     'max' => 8, 'tooLong' => 'lang最长8位'],
            ['variable_name', 'required', 'message' => 'variable_name不能为空', 'on' => 'add'],
            ['variable_name', 'string', 'message' => 'variable_name必须是字符串',
                                'min' => 2, 'tooShort' => 'variable_name最短2位',
                                'max' => 16, 'tooLong' => 'variable_name最长16位'],
            ['variable_tag', 'validateTag', 'on' => 'add'],
            ['variable_tag', 'required', 'message' => 'variable_tag不能为空', 'on' => 'add'],
            ['variable_tag', 'string', 'message' => 'variable_tag必须是字符串',
                                'min' => 3, 'tooShort' => 'variable_tag最短3位',
                                'max' => 16, 'tooLong' => 'variable_tag最长16位'],
            ['variable_val', 'default', 'value' => ''],
            ['variable_val', 'string', 'message' => 'variable_val必须是字符串',
                                'min' => 1, 'tooShort' => 'variable_val最短1位',
                                'max' => 512, 'tooLong' => 'variable_val最长512位'],
            ['flag', 'integer', 'message' => 'flag必须为数字，或数字字符串'],
            ['flag', 'in', 'range' => array(1, 2), 'message' => 'flag值必须在1,2中选择'],
            ['sysval', 'integer', 'message' => 'sysval必须为数字，或数字字符串'],
            ['sysval', 'in', 'range' => array(1, 2), 'message' => 'sysval值必须在1,2中选择'],
            ['lastmodify_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['lastmodify_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastmodify_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /*rules 验证登录信息是否吻合*/
    public function validateTag()
    {
        if(!$this->hasErrors()){
            $data = self::find()->where('app_id = :app_id and lang = :lang and variable_tag = :tag', [':app_id' => $this->app_id, ':lang' => $this->lang, ':tag' => $this->variable_tag])->asArray()->one();
            // P($data);
            if(!is_null($data)){
                $this->addError('variable_name_repeat', '标签名称重复了');
            }
        }
    }


    /*添加*/
    public function addVariable($data, $app_id)
    {
        $this->scenario = 'add';
        $data['GlobalVariable']['app_id'] = $app_id;
        $data['GlobalVariable']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyVariable($data, $id)
    {
        // P($data);
        $globalVariable = self::find()->where('id = :id', [':id' => $id])->one();
        // P($globalVariable);
        if(!is_null($globalVariable)){
            if($this->load($data) and $this->validate()){
                if(isset($this->variable_val)){
                    $globalVariable->variable_val = $this->variable_val;
                }
                $globalVariable->lastmodify_time = date('Y-m-d H:i:s', time());
                // P($globalVariable);
                if($globalVariable->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}